########################################################
#
# 
#
# Description : 统计本地代码提交行数
#
########################################################

#!/bin/bash
BuyLogo='
=====================================================                                                                      
		欢迎查询代码工作量            
 使用说明： 
 使用前请将本脚本放到自己 workSpase下 鼠标左键使用git bash here
 打开 使用命令 sh code.sh 打开
 
                               email:1360105504@qq.com                     
 by zhou xin zhi               version 2.0 2022/06/02        
====================================================='; 
NameLogo='
=====================================================                                                                      
Ennn........请输入项目名字查询				
====================================================='; 

ErrerLogo='
=====================================================                                                                      
				workspase没有项目哦！				
====================================================='; 

 

########################################################
# 以下代码不需动                       
########################################################

export index=0             			#记录当前的位置
export add_line_count=0             #添加的line总行数
export remove_line_count=0          #删除的总行数

export array_git_repositories=()    #用于记录仓库名
export add_code=()                  #记录所有用户对某个库的添加的行数
export remove_code=()               #记录所有用户对某个库的删除的行数



#对命令执行的返回值进行数据切割
function get_add_remove_count() {
	string=$1
	array=(${string//,/ })
	if [ ! ${array[0]} ]; then
  		add_line=0
	else
  		add_line=${array[0]}
	fi
	
	if [ ! ${array[1]} ]; then
  		remove_line=0
	else
  		remove_line=${array[1]}
	fi

	if [ ! ${add_code[$index]} ]; then
  		add_code[$index]=0
	
	fi
	if [ ! ${remove_code[$index]} ]; then
  		remove_code[$index]=0
	
	fi
	remove_code[$in dex]=`expr ${remove_code[$index]} + $remove_line`
	add_code[$index]=`expr ${add_code[$index]} + $add_line`

	echo "用户"$2"添加了="$add_line"行 删除了"$add_line"行"
	
}
#获取该用户在该文件夹下的提交代码数
function get_user_line() {

	# output分别去接收 该文件夹下的提交以及删除行数
	#output=$(git log --all  --since="$actiondate" --before="$thisDate" --author=${1}  --pretty=tformat: --numstat | awk '
    #{add += $1; subs += $2; loc += $1 - $2 } END { printf "%s,%s,%s\n", add, subs, loc }' -)
	output=$(git log --all  --since="$actiondate" --until="$thisDate" --author=$(git config --get user.name)  --pretty=tformat: --numstat | awk '
	{add += $1; subs += $2; loc += $1 - $2 } END { printf "%s,%s,%s\n", add, subs, loc }' -)
	get_add_remove_count $output ${1} ${actiondate} ${thisDate}
}




echo -e "\033[36m$BuyLogo\033[0m"
 #users_name=()   
#echo -e -n "\033[34m请输入查询的git名字：\033[0m"
#read userName
# if [ -z "$userName" ]; then
# echo -e "\033[36m$NameLogo\033[0m"
#exit 1
# else
echo "
------------------------
1 - 查询周代码量
2 - 查询今天代码量
3 - 查询月代码量
4 - 查询上月代码量
5 - 查询昨天代码量
6 - 查询指定项目代码量(查询当天)
------------------------
"
read -p "请输入对应的数字并回车（默认查询月代码量）:" a

case $a in
    1)  echo '你选择了 1 查询周代码量'
	
	 actiondate=`for((i=0;i<7;i++)); do if [ \`date -d "-$i days" +%w\` = 1 ]; then date -d "-$i days" +%Y-%m-%d; fi; done`
	 thisDate=`date +%Y-%m-%d`
	  echo $actiondate'至'  $thisDate 
	
    ;;
    2)  echo '你选择了 2 查询今天代码量' 
	# actiondate=$(date -d "yesterday" +%Y-%m-%d )" 23:59:59"
	  actiondate=$(date +%Y-%m-%d )" 00:00:00"
	 thisDate=$(date "+%Y-%m-%d 23:59:59")
	  #actiondate="2022-06-02 00-00-00"
	 #thisDate="2022-06-02 23-59-59"
	 echo $actiondate '至' $thisDate
    ;;
    3)  echo '你选择了 3 查询月代码量'
	thisDate=`date "+%Y-%m-%d"`
	actiondate=`date +%Y-%m-01`
     echo $actiondate '至'  $thisDate
	;; 
    4)  echo '默认执行 查询上月代码量'
	#$actiondate=$(date -d"1 day ago $(date -d "yesterday" +%Y-%m-%d )" +%Y-%m-%d)
	actiondate=`date -d last-month  +%Y-%m-01`
	thisDate=`date -d "-$(date +%d) days" +%Y-%m-%d`
	 echo $actiondate'至' $thisDate 
	;;
	5)  echo '你选择了 5 查询昨天代码量'
	thisDate=$(date -d "yesterday" +%Y-%m-%d )" 23:59:59"
	#actiondate=$(date -d"1 day ago $(date -d "yesterday" +%Y-%m-%d )" +%Y-%m-%d)" 00:00:00"
	actiondate=$(date -d "yesterday" +%Y-%m-%d )" 00:00:00"
     echo $actiondate '至'  $thisDate
	;;
	6)  echo '你选择了 6 查询指定项目代码量(查询当天)'
		actiondate=$(date +%Y-%m-%d )" 00:00:00"
		thisDate=$(date "+%Y-%m-%d 23:59:59")
		address=()   
		echo -e -n "\033[34m请输入项目全路径：\033[0m"
		read address
		 if [ -z "$address" ]; then
		 echo -e "\033[36m$NameLogo\033[0m"
		exit 1
		 else 
		 	cd $address
		 	index=0; #用于记录当前在第几个文件夹下处理
            array_git_repositories=(${array_git_repositories[@]} $address)
		echo $actiondate '至'  $thisDate
		   echo "<=========获取【"$address"】的Git代码提交数据======>"
		   	get_user_line  $userName
		all_add_line=0
		all_remove_line=0
		echo '==============================================================================='
		echo "                  本次共统计了【"${#array_git_repositories[@]}"】个仓库         "
		echo '==============================================================================='
		printf "%-30s %10s %10s %10s\n" "Folder" "Add" "Remove" "All"
		echo '-------------------------------------------------------------------------------'
		for ((i=0;i<${#array_git_repositories[@]};i++))
		do
			all_add_line=`expr $all_add_line + ${add_code[$i]}`
			all_remove_line=`expr $all_remove_line + ${remove_code[$i]}`
			printf "%-30s %10s %10s %10s\n" ${array_git_repositories[$i]} ${add_code[$i]} ${remove_code[$i]} `expr ${add_code[$i]} - ${remove_code[$i]}`
		done
		echo '-------------------------------------------------------------------------------'
		printf "%-30s %10s %10s %10s\n" "Total" $all_add_line $all_remove_line `expr $all_add_line - $all_remove_line`
		echo '==============================================================================='
				exit 1
		fi
	;;
    *)  echo '默认执行 查询月代码量'
	thisDate=`date "+%Y-%m-%d"`
	actiondate=`date +%Y-%m-01`
	#actiondate=`date -d '1 days ago' +%Y-%m-%d`
     echo $actiondate '至'  $thisDate
    ;;
	
esac
 #users_name=($userName)  
 for path in `ls -l $(dirname $0)|awk -F " " '{print $9}'`
do
    if [ -d $path ]
    then
            echo "<=========获取【"$path"】的Git代码提交数据======>"
			index=${#array_git_repositories[@]} #用于记录当前在第几个文件夹下处理
            array_git_repositories=(${array_git_repositories[@]} $path)
            cd $path
				#get_user_line  $userName
				get_user_line  $userName
			cd ..
		fi
	
done
all_add_line=0
all_remove_line=0
echo '==============================================================================='
echo "                  本次共统计了【"${#array_git_repositories[@]}"】个仓库         "
echo '==============================================================================='
printf "%-30s %10s %10s %10s\n" "Folder" "Add" "Remove" "All"
echo '-------------------------------------------------------------------------------'
for ((i=0;i<${#array_git_repositories[@]};i++))
do
	all_add_line=`expr $all_add_line + ${add_code[$i]}`
	all_remove_line=`expr $all_remove_line + ${remove_code[$i]}`
	printf "%-30s %10s %10s %10s\n" ${array_git_repositories[$i]} ${add_code[$i]} ${remove_code[$i]} `expr ${add_code[$i]} - ${remove_code[$i]}`
done
echo '-------------------------------------------------------------------------------'
printf "%-30s %10s %10s %10s\n" "Total" $all_add_line $all_remove_line `expr $all_add_line - $all_remove_line`
echo '==============================================================================='
